﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Reflection;
using System.Reflection.Emit;
using MapSurface.UIModelElement;
using MapSurface.AssemblyReflexion;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace MapSurface.WidgetSurface
{
    public class XamlObjects
    {
        #region Properties
        private object _myContext;
        private Object _xamlRootDocument;
        private Object _designRootElement;
        private Object _designSpace = null;
        private MapSurface.AssemblyReflexion.MethodBuilder _mBuilder;
        public TextBlock message;
        public Object DesignSpace
        {
            get { return _designSpace; }
            set { _designSpace = value; }
        }

        public Object DesignRootElement
        {
            get { return _designRootElement; }
            set { _designRootElement = value; }
        }
        private AssemblyReflexion.AssemblyReflexion _assembly;

        public AssemblyReflexion.AssemblyReflexion Assembly
        {
            get { return _assembly; }
            set { _assembly = value; }
        }

        private APIModelManager apiModel;

        public ChoiceManager widgetChoice;
        #endregion
        
        #region Constructors
        public XamlObjects(string choiceWidget,string dllDir,string apiFile, List<Delegate> selectHanlder ,object desingspace)
        {
            _assembly = new AssemblyReflexion.AssemblyReflexion(dllDir, selectHanlder);
            apiModel = new APIModelManager(apiFile);
            _designSpace = desingspace;
            try
            {

                widgetChoice = new ChoiceManager(choiceWidget, dllDir, selectHanlder);
                widgetChoice.apiModel = apiModel;
               
            }
            catch (Exception)
            {
                widgetChoice = null;

            }
 
        }
	    #endregion
        
        #region XAML Object creation
        public List<Object> build(UIModel model)
        {
            List<Object> uiobject= new List<object>();
            if(model!=null  && model.Contains!=null ){
                foreach (MapSurface.UIModelElement.UIElement element in model.Contains)
                {
                    Object instance =  null;
                    try
                    {
                        object ret = visit((Container)element, instance);
                        if(ret!=null)
                            uiobject.Add(ret );
                    }catch(InvalidCastException )
                    {
                        object ret = visit(element, instance);
                        if(ret!=null)
                            uiobject.Add(ret );
                    }
                }
                _xamlRootDocument = _assembly.getInstance("SurfaceWindow", model.Contains[0]);
                if (uiobject.Count > 0)
                    _designRootElement = uiobject[0];
                else
                    _designRootElement = null;
             }
            return uiobject;
        }
        public Object visit(Container current, Object container)
        {
            string name = current.WidgetType.Name;
            int i = 0;
            object objectUI = _assembly.getInstance(name, current);
            current.XalmObject = objectUI;
            if (container == null)
                container = objectUI;
            if (name.Contains("SurfaceWindow") || name.CompareTo("Page")==0)
            {
                container = null;
                objectUI = null;
            }
            if(current.Name.Contains("Grid"))
            {
            }
            if(current.Contains!=null)
            foreach (MapSurface.UIModelElement.UIElement ue in current.Contains)
            {
                Object obj = null;
                try
                {
                    obj = visit((Container)ue, objectUI);    
                }
                catch (InvalidCastException e)
                {
                    obj = visit(ue, objectUI);
                }
                if (i == 0 && _assembly.getChild(objectUI).Count == 1 && current.WidgetType.ContainsWidgets!=null )
                {
                    _assembly.removeChild(ref objectUI, _assembly.getChild(objectUI)[0]);
                    i++;
                }
                objectUI = _assembly.addChild(objectUI, obj);
            }
            if (objectUI != null && objectUI.GetType().Name.CompareTo("BorderElement") != 0 && !objectUI.GetType().Name.Contains("SurfaceWindow") && !apiModel.isContainsWidget(current.WidgetType.Name))
            {
                return new BorderElement((Canvas)_designSpace, objectUI);
            }
            else
            {
                return objectUI;
            }
        }
        public object visit(MapSurface.UIModelElement.UIElement current, Object container)
        {
            if (current != null )
            {
                object uiElement=_assembly.getInstance(current.WidgetType.Name, current);
                if (current.Name.Contains("image"))
                {
                }
                current.XalmObject= uiElement;
                if (!apiModel.isContainsWidget(current.WidgetType.Name))
                    return new BorderElement((Canvas)_designSpace, uiElement);
                else 
                    return uiElement;
            }
            else return null;
        }
        private object getUIObject(object current)
        {
            if (current != null && current.GetType().Name.CompareTo("BorderElement") == 0)
                return ((BorderElement)current).Content.Children[0];
            return current;
        }
        private XmlDocument visitEventAdd(MapSurface.UIModelElement.Container current, XmlDocument root)
        {
            string exp = "//*";
            XmlNodeList xn = root.DocumentElement.SelectNodes(exp);

            Property content = current.getContentProperty();
            if (xn.Count > 0)
            foreach (XmlNode xni in xn)
            {
                if (xni.Attributes["Name"] != null && xni.Attributes["Name"].Value.CompareTo(current.Name) == 0)
                {
                    if(current.Events!=null)
                        foreach (Event e in current.Events)
                        {
                            XmlAttribute typeAttr = root.CreateAttribute(e.Name);
                            typeAttr.Value = e.ScriptCode;
                            xni.Attributes.Append(typeAttr);
                        }
                    if (content != null && xni.Attributes[content.Name] != null)
                        xni.Attributes[content.Name].Value = content.Value;

                }
            }
            if (current.Contains != null)
                foreach (MapSurface.UIModelElement.UIElement e in current.Contains)
                    try
                    {
                        root=visitEventAdd((Container)e, root);
                    }
                    catch (Exception)
                    {
                        
                         root=visitEventAdd(e, root);
                    }
            
            return root;

        }
        private XmlDocument visitEventAdd(MapSurface.UIModelElement.UIElement current, XmlDocument root)
        {
            string exp = "//*";
            XmlNodeList xn = root.DocumentElement.SelectNodes(exp);
            Property content = current.getContentProperty();
            if(xn.Count>0)
            foreach (XmlNode xni in xn )
            {
                if (xni.Attributes["Name"] != null && xni.Attributes["Name"].Value.CompareTo(current.Name) == 0)
                {
                    if(current.Events!=null)
                    foreach (Event e in current.Events)
                    {
                        XmlAttribute typeAttr = root.CreateAttribute(e.Name);
                        typeAttr.Value = e.ScriptCode;
                        xni.Attributes.Append(typeAttr);
                    }
                    if (content != null && xni.Attributes[content.Name] != null)
                        xni.Attributes[content.Name].Value = content.Value;

                }
            }
            return root;

        }
        public void concretise(String fileName,Object current )
        {
            //
            String obj = XamlWriter.Save(_designRootElement);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(obj);
            object newRoot = parseXlm(doc.DocumentElement);
            using (System.IO.FileStream fs = System.IO.File.Create(fileName))
            {
                StreamWriter outfile = new StreamWriter(fs);
                String contains = XamlWriter.Save(newRoot);
                Console.Out.WriteLine(contains);
                outfile.Write(getWindowsTag(contains));
                outfile.Close();
            }
            XmlDocument docu =new XmlDocument();
            docu.Load(fileName);
            //add Fonctionna Event Event
            docu=visitEventAdd((MapSurface.UIModelElement.Container)current,docu);
            docu.Save(fileName);
            
        }
        private object parseXlm(XmlNode node)
        {
            XmlNode xnode = null;
            XmlNode margin = null;
            if(node.Attributes!=null && node.Attributes.GetNamedItem("Margin")!=null)
                margin =node.Attributes.GetNamedItem("Margin");
            object instance = null;
            List<Object> child = null;
            if (node.Name.Contains("BorderElement"))
            {
                foreach (XmlNode xn in node.ChildNodes)
                    if (xn.Name.Contains("Grid"))
                    {
                        xnode = xn.ChildNodes[0];
                        node.RemoveChild(xn);
                        xn.RemoveChild(xnode);
                        if (xnode.HasChildNodes)
                            foreach (XmlNode ci in xnode)
                            {
                                if (child == null)
                                    child = new List<object>();
                                if(!child.Contains(ci))
                                    if(!ci.Name.Contains('.'))
                                        child.Add(parseXlm(ci));
                            }
                    }
            }
            else
            {
                foreach (XmlNode xn in node.ChildNodes)
                    if (xn.Name.Contains("BorderElement"))
                    {
                        if (child == null)
                            child = new List<object>();
                        if (!child.Contains(xn))
                            if (!xn.Name.Contains('.'))
                                child.Add(parseXlm(xn));
                    }
                xnode = node;
            }

            if (xnode == null)
                return null;
          
            if(xnode.HasChildNodes)
            {
                List<XmlNode> nodel = new List<XmlNode>();
                while (xnode.HasChildNodes)
                {
                    XmlNode x = xnode.FirstChild;
                    if (x.InnerXml.Length == 0 && xnode.InnerText.Length!=0)
                        nodel.Add(xnode.RemoveChild(x));
                    else
                        xnode.RemoveChild(x);                    
                }
                if (nodel.Count > 0)
                    foreach (XmlNode x in nodel)
                        if(x.ParentNode==null)
                            xnode.AppendChild(x);
            }

            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace(xnode.Prefix, xnode.NamespaceURI);

            // Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
            ParserContext pc = new ParserContext(context);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlReader reader = XmlReader.Create(new StringReader(xnode.OuterXml), settings, context);
            instance = XamlReader.Load(reader);
            if (child != null)
                instance = _assembly.addChilds(instance, child);
            if (margin != null)
            {
                string [] mar=margin.Value.Split(',');
                if(mar.Length<1)
                    mar=margin.Value.Split(';');
                Thread.CurrentThread.CurrentCulture= CultureInfo.CreateSpecificCulture("en-US");
                NumberStyles styles;
                styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign | NumberStyles.Float | NumberStyles.AllowThousands;    
                Thickness t = new Thickness(Double.Parse(mar[0], styles), double.Parse(mar[1], styles), double.Parse(mar[2], styles), double.Parse(mar[3], styles));
                _assembly.setAttribute(ref instance, "Margin", t);
            }
            return instance;
   
        }
        private String getWindowsTag(string contains)
        {
            string tag = "<s:SurfaceWindow x:Class=\"SurfaceApplication1.SurfaceWindow1\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"  xmlns:s=\"http://schemas.microsoft.com/surface/2008\"   Title=\"SurfaceApplication1\"   >";
            tag+="<s:SurfaceWindow.Resources>  <ImageBrush x:Key=\"WindowBackground\" Stretch=\"None\" Opacity=\"0.6\" ImageSource=\"pack://application:,,,/Resources/WindowBackground.jpg\"/> </s:SurfaceWindow.Resources>";
            tag += " <Grid Background=\"{StaticResource WindowBackground}\" >";
            tag += contains;
            tag += "</Grid></s:SurfaceWindow>";

            return tag;
        }
        
        public object removeEmpty (object uiRoot)
        {
            object newRoot = null;
            List<object> child = new List<object>();
            if (uiRoot.GetType().Name.CompareTo("BorderElement") == 0 && ((BorderElement)uiRoot).Content != null)
                newRoot = ((BorderElement)uiRoot).Content.Children[0];
            else
                newRoot = uiRoot;
            child = _assembly.getChild(newRoot);
            foreach (object o in child)
                newRoot = _assembly.addChild(newRoot,o);
            return newRoot;
        }

        #endregion   
        
        #region Operations

        public List<Object> getChoiceInstance(object instance)
        {
            if (widgetChoice == null)
                return null;
            Widget w = apiModel.findWidget(instance.GetType().Name);

            if (w != null )
                return widgetChoice.getEquivalentInstance(w, instance);
            return null;
        }
        

        public int replaceObject(ref object rootUi,object newObject, object instance, bool all)
        {
            object dad = BorderElement.getBorderElement(instance);
            newSame = newObject;
            if(!all)
                return replaceUIObject(ref rootUi, ref dad, ref newObject, instance.GetHashCode(), newSame);
            replaceAllUIObject(rootUi, ref dad, newObject, instance.GetType(), newSame);
            //widgetChoice.replaceInstance(instance, newObject);
            return 0;
        }
        private int replaceUIObject(ref object root, ref object dad, ref object newInstance, int hashCode, object newSame)
        {
            if (hashCode == root.GetHashCode())
            {
                return 0;
            }
            else 
            {
                try
                {
                    if (((ItemsControl)root).Items.Count > 0)
                        for (int i = 0; i < ((ItemsControl)root).Items.Count; i++)
                        {
                            object c;
                            int ret;
                            newInstance = ItemControlBorderTeste(root, newInstance, hashCode, i, out c, out ret);
                            if (c != null && ret == 0)
                            {
                                newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                return 1;
                            }
                            else if (ret > 0)
                                return 1;
                        }
                }
                catch (InvalidCastException)
                {
                    try
                    {
                        if (((Panel)root).Children.Count > 0)
                            for (int i = 0; i < ((Panel)root).Children.Count; i++)
                            {
                                object c;
                                int ret;
                                newInstance = PanelBorderControl(root, newInstance, hashCode, i, out c, out ret);
                                if (c != null && ret==0)
                                {
                                    newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                    return 1;
                                }
                                else if (ret > 0)
                                    return 1;

                            }
                    }
                    catch (InvalidCastException   )
                    {
                        try
                        {
                            if (((System.Windows.Controls.Decorator)root).Child != null)
                            {
                                object c;
                                int ret;
                                newInstance = DecoratorCorderTeste(root, newInstance, hashCode, out c, out ret);
                                if (c != null && ret == 0)
                                {
                                    newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                    return 1;
                                }
                                else if (ret > 0)
                                    return 1;

                            }
                        }
                        catch (InvalidCastException )
                        {
                            try
                            {
                                if (((ContentControl)root).Content != null)
                                {
                                    object c;
                                    int ret;
                                    c = ((ContentControl)root).Content;
                                    Object current = null;
                                    if (c.GetType().Name.CompareTo("BorderElement") == 0)
                                    {
                                        current = (BorderElement)c;
                                        c = ((BorderElement)c).Content;

                                    }
                                    ret = replaceUIObject(ref c, ref current, ref newInstance, hashCode, newSame);
                                    if (c != null && ret == 0)
                                    {
                                        newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                        return 1;
                                    }
                                    else if (ret > 0)
                                        return 1;

                                }
                            }
                            catch (InvalidCastException )
                            {


                            }


                        }
                    }
                }
                catch (Exception e)
                {
                    throw new  NotSupportedException();
                }
                
            }
            return -1;
        }

        private int replaceAllUIObject(object root, ref object dad, object newInstance, Type type, object newSame)
        {
            if (type == root.GetType())
            {
                return 0;
            }
            else
            {
                try
                {
                    if (((ItemsControl)root).Items.Count > 0)
                        for (int i = 0; i < ((ItemsControl)root).Items.Count; i++)
                        {
                            object c;
                            int ret;
                            newInstance = ItemControlBorderTeste(root, newInstance, type, i, out c, out ret);
                            if (c != null && ret == 0)
                            {
                                newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                            }
                        }
                }
                catch (InvalidCastException)
                {
                    try
                    {
                        if (((Panel)root).Children.Count > 0)
                            for (int i = 0; i < ((Panel)root).Children.Count; i++)
                            {
                                object c;
                                int ret;
                                newInstance = PanelBorderControl(root, newInstance, type, i, out c, out ret);
                                if (c != null && ret == 0)
                                {
                                    newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                }

                            }
                    }
                    catch (InvalidCastException)
                    {
                        try
                        {
                            if (((System.Windows.Controls.Decorator)root).Child != null)
                            {
                                object c;
                                int ret;
                                newInstance = DecoratorCorderTeste(root, newInstance, type, out c, out ret);
                                if (c != null && ret == 0)
                                {
                                    newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                }

                            }
                        }
                        catch (InvalidCastException)
                        {
                            try
                            {
                                if (((ContentControl)root).Content != null)
                                {
                                    object c;
                                    int ret;
                                    c = ((ContentControl)root).Content;
                                    Object current = null;
                                    if (c.GetType().Name.CompareTo("BorderElement") == 0)
                                    {
                                        current = (BorderElement)c;
                                        c = ((BorderElement)c).Content;

                                    }
                                    ret = replaceAllUIObject(c, ref current, newInstance, type, newSame);
                                    if (c != null && ret == 0)
                                    {
                                        newInstance = replaceInstance(newInstance.GetType(), ref  c, ref  dad, newSame);
                                    }

                                }
                            }
                            catch (InvalidCastException)
                            {


                            }


                        }
                    }
                }
                catch (Exception)
                {
                    throw new NotSupportedException();
                }

            }
            return -1;

        }

        
        #region Replace Private methods
        private object newSame = null;
        private object DecoratorCorderTeste(object root, object newInstance, int hashCode, out object c, out int ret)
        {
            c = ((System.Windows.Controls.Decorator)root).Child;
            Object current = null;
            if (c.GetType().Name.CompareTo("BorderElement") == 0)
            {
                current = (BorderElement)c;
                c = ((BorderElement)c).Content;

            }
            ret = replaceUIObject(ref c, ref current, ref newInstance, hashCode,newSame);
            return newInstance;
        }

        private object PanelBorderControl(object root, object newInstance, int hashCode, int i, out object c, out int ret)
        {
            c = ((Panel)root).Children[i];
            Object current = null;
            if (c.GetType().Name.CompareTo("BorderElement") == 0)
            {
                current = (BorderElement)c;
                c = ((BorderElement)c).Content;

            }
            ret = replaceUIObject(ref c, ref current, ref newInstance, hashCode,newSame);
            return newInstance;
        }

        private object ItemControlBorderTeste(object root, object newInstance, int hashCode, int i, out object c, out int ret)
        {
            c = ((ItemsControl)root).Items[i];
            Object current = null;
            if (c.GetType().Name.CompareTo("BorderElement") == 0)
            {
                current = c;
                c = ((BorderElement)c).Content.Children[0];

            }
            ret = replaceUIObject(ref c, ref current, ref newInstance, hashCode, newSame);
            return newInstance;
        }

        private void replaceInCollection(ref object newObject,ref  object instance )
        {
            if (((System.Windows.FrameworkElement)instance).Parent != null)
            {
                foreach (BorderElement b in BorderElement.BorderCollection)
                {
                    if (b.Content.Children.Contains((System.Windows.UIElement)instance))
                    {
                        b.setBorderVisible();
                        b.wr.CanResize = false;
                        b.wm.CanMove = false;
                        b.objectUI = newObject;
                        b.Content.Children.Remove((System.Windows.UIElement)instance);
                        b.Content.Children.Add((System.Windows.UIElement)newObject);
                        break;
                    }

                }
            }

        }


        private object DecoratorCorderTeste(object root, object newInstance, Type type, out object c, out int ret)
        {
            c = ((System.Windows.Controls.Decorator)root).Child;
            Object current = null;
            if (c.GetType().Name.CompareTo("BorderElement") == 0)
            {
                current = (BorderElement)c;
                c = ((BorderElement)c).Content;

            }
            ret = replaceAllUIObject(c, ref current, newInstance, type, newSame);
            return newInstance;
        }

        private object PanelBorderControl(object root, object newInstance, Type type, int i, out object c, out int ret)
        {
            c = ((Panel)root).Children[i];
            Object current = null;
            if (c.GetType().Name.CompareTo("BorderElement") == 0)
            {
                current = (BorderElement)c;
                c = ((BorderElement)c).Content;

            }
            ret = replaceAllUIObject(c, ref current, newInstance, type, newSame);
            return newInstance;
        }

        private object ItemControlBorderTeste(object root, object newInstance, Type type, int i, out object c, out int ret)
        {
            c = ((ItemsControl)root).Items[i];
            Object current = null;
            if (c.GetType().Name.CompareTo("BorderElement") == 0)
            {
                current = c;
                c = ((BorderElement)c).Content.Children[0];

            }
            ret = replaceAllUIObject( c,ref current,   newInstance, type, newSame);
            return newInstance;
        }

        private object replaceInstance(  Type type, ref object oldObject, ref object dad , object newSame)
        {
            object newInstance= _assembly.getInstance(apiModel.findWidget(type.Name));
            _assembly.setAttribute(ref newInstance, "Name", _assembly.getAttribute(oldObject,"Name"));
            _assembly.setAttribute(ref newInstance, newSame);
            List<object> childs = _assembly.RemoveChid(ref oldObject);
            Widget wi=apiModel.findWidget(newInstance.GetType().Name);
            Widget wo = apiModel.findWidget(oldObject.GetType().Name);
            if (wi.ContainsWidgets != null && childs != null)
            {
                int j = 0;
                foreach (object o in childs)
                {
                    object item = _assembly.getInstance(wi.ContainsWidgets);
                    item = _assembly.addChild(item, o);
                    if (j == 0 && _assembly.getChild(newInstance).Count==0)
                        _assembly.removeChild(ref newInstance, _assembly.getChild(newInstance)[0]);
                    j++;
                    newInstance = _assembly.addChild(newInstance, item);
                }
                if (childs.Count == 0)
                {
                    object item = _assembly.getInstance(wi.ContainsWidgets);
                    _assembly.setAttribute(ref item, wi.ContainsWidgets, wo, oldObject);
                    newInstance = _assembly.addChild(newInstance, item);

                }

            }
            else
            {
                foreach (object o in childs)
                    if (wo.ContainsWidgets != null && o.GetType().Name.CompareTo(wo.ContainsWidgets.Name) == 0)
                    {
                        object ol = o;
                        List<object> nc = _assembly.getChild(ol);
                        if (nc != null && nc.Count > 0)
                        {
                            _assembly.removeChild(ref ol, nc[0]);
                            newInstance = _assembly.addChild(newInstance, nc[0]);
                        }
                    }
                    else if(wo.ContainsWidgets != null &&  o.GetType().Name.CompareTo("BorderElement") == 0)
                    {
                        object ol =((BorderElement)o).objectUI;
                         List<object> nc = _assembly.getChild(ol);
                        if (nc != null && nc.Count > 0)
                        {
                            _assembly.removeChild(ref ol, nc[0]);
                            newInstance = _assembly.addChild(newInstance, nc[0]);
                        }
                    }
            }

            String s=_assembly.getAttribute(oldObject, "RenderSize");

            String w = "", h = "";
            if (s != null)
            {
                string[] siz = s.Split(';');
                if (siz.Length < 2)
                    siz = s.Split(',');
                w = siz[0]; h = siz[1];
                System.Windows.Size size = new System.Windows.Size(double.Parse(w), double.Parse(h));
                _assembly.setAttribute(ref newInstance, "Width", ((System.Windows.Size)size).Width);
                _assembly.setAttribute(ref newInstance, "Height", ((System.Windows.Size)size).Height);

            }
            try
            {
                ((BorderElement)dad).wm.deConnectMouseHandlers((BorderElement)dad);
            }
            catch (Exception)
            {
            }
            ((BorderElement)dad).wm.connectMouseHandlers((BorderElement)dad);
            ((BorderElement)dad).Content.Children.Remove((System.Windows.UIElement)oldObject);
            
            dad = _assembly.removeVisualChid(dad, oldObject);
            ((BorderElement)dad).Content.Children.Add((System.Windows.UIElement)newInstance);
            ((BorderElement)dad).objectUI = newInstance;
            int i = BorderElement.BorderCollection.IndexOf((BorderElement)dad);
            BorderElement.BorderCollection.Insert(i, (BorderElement)dad);

            return newInstance;
        }
        #endregion

        public bool deleteXamlElement(ref object root, object instance)
        {
            _assembly.deleteXamlObject(ref root, instance);
            return true;
        }



        #endregion

    }
}
